//
//  ESPhotoDetailViewController.m
//  Eyeshot
//
//  Created by admin on 15/7/2.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESPhotoDetailViewController.h"

@interface ESPhotoDetailViewController ()<UIScrollViewDelegate, UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIScrollView *scrollView;
@end

@implementation ESPhotoDetailViewController

- (UIImageView *)imageV
{
    if (!_imageV) {
        _imageV = [[UIImageView alloc] init];
    }
    return _imageV;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self createScrollView];
    //创建返回button
    self.backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.backButton.frame = CGRectMake(50, 50, 20, 20);
    [self.backButton setBackgroundImage:[UIImage imageNamed:@"editer_ButtonReturn"] forState:(UIControlStateNormal)];
    self.backButton.backgroundColor  = [UIColor lightGrayColor];
  
    [self.backButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];

    //创建imageView
    CGFloat imagew = self.imageV.image.size.width;
    CGFloat imageh = self.imageV.image.size.height;
     CGFloat imageW = self.view.width;
    CGFloat imageH = imageh * imageW / imagew;
    
    if (imageH <= self.view.height)
    {
      
        self.imageV.height = imageH;
        self.imageV.width = imageW;
        self.imageV.center = self.view.center;

    }
    else
    {
        self.imageV.y = 0;
        self.imageV.x = 0;
        self.imageV.height = imageH;
        self.imageV.width = imageW;

        self.scrollView.contentSize = CGSizeMake(self.scrollView.width, imageH);
        
    }
   
    [self.scrollView  addSubview:self.imageV];
    [self.view addSubview:self.backButton];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //创建转发、保存按钮
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [button setTitle:@"转发" forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    button.backgroundColor = [UIColor grayColor];
    [button addTarget:self action:@selector(transmit:) forControlEvents:(UIControlEventTouchUpInside)];
    button.frame = CGRectMake(kScreenW - 100, kScreenH - 50, 40, 20);
    button.alpha = 0.5;
    [self.view addSubview:button];
    
    UIButton *saveBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [saveBtn setTitle:@"保存" forState:(UIControlStateNormal)];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    saveBtn.backgroundColor = [UIColor grayColor];
     saveBtn.alpha = 0.5;
    [saveBtn addTarget:self action:@selector(save:) forControlEvents:(UIControlEventTouchUpInside)];
     saveBtn.frame = CGRectMake(kScreenW - 50, kScreenH - 50, 40, 20);
    [self.view addSubview:saveBtn];
    
}

//转发按钮的事件
- (void)transmit:(UIButton *)btn
{
    NSLog(@"转发");
}
//保存按钮的事件
- (void)save:(UIButton *)btn
{
    NSLog(@"保存");

}

//创建一个scrollView
- (void)createScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.backgroundColor = [UIColor blackColor];
   
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    scrollView.maximumZoomScale = 3.0;
    scrollView.minimumZoomScale = 0.5;
    scrollView.delegate = self;

}


//让本控制器消失
- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//scorllView的代理方法
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageV;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    if (scrollView.zoomScale <= 1)
    {
        self.imageV.center = self.view.center;
    }
}


@end
